package com.foundersoftware.commons.organization.dao.impl;

import com.foundersoftware.commons.organization.InvalidRoleTypeException;
import com.foundersoftware.commons.organization.ObjectAlreadyExistsException;
import com.foundersoftware.commons.organization.ObjectNotFoundException;
import com.foundersoftware.commons.organization.OrganizationException;
import com.foundersoftware.commons.organization.OrganizationManager;
import com.foundersoftware.commons.organization.dao.IOrganizationDAO;
import com.foundersoftware.commons.organization.model.AbstractOrganizationObject;
import com.foundersoftware.commons.organization.model.BusinessLeader;
import com.foundersoftware.commons.organization.model.Employee;
import com.foundersoftware.commons.organization.model.OrganizationUnit;
import com.foundersoftware.commons.organization.model.OrganizationUnitType;
import com.foundersoftware.commons.organization.model.Position;
import com.foundersoftware.commons.organization.model.PositionType;
import com.foundersoftware.commons.organization.model.Responsibility;
import com.foundersoftware.commons.organization.model.Role;
import com.foundersoftware.commons.organization.model.RoleType;
import com.foundersoftware.commons.organization.model.Type;
import com.foundersoftware.commons.organization.model.WorkGroup;
import com.foundersoftware.commons.organization.model.WorkGroupType;

/**
<code>{@link com.foundersoftware.commons.organization.OrganizationManager}</code>
 *
 * @author wuhongjun@routon.com
 * @version 3.0
 */
public class BasicOrganizationManager implements OrganizationManager {
    /**
	 * 
	 */
	private static final long serialVersionUID = 2211373507663833456L;
	private IOrganizationDAO organizationStore;

    public BasicOrganizationManager(IOrganizationDAO organiztionStore) {
        if (organiztionStore == null) {
            throw new IllegalArgumentException("��֯��洢����Ϊnull��");
        }

        this.organizationStore = organiztionStore;

    }

    public BasicOrganizationManager() {
    }

    public Employee createEmployee(Employee employee) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (employee == null) {
            throw new IllegalArgumentException("�����Աemployee����Ϊ��");
        }
        organizationStore.storeEmployee(employee, true);
        return employee;

    }

    public OrganizationUnitType createOrganizationUnitType(OrganizationUnitType
            organizationUnitType) throws ObjectAlreadyExistsException,
            OrganizationException {
        if (organizationUnitType == null) {
            throw new IllegalArgumentException(
                    "������֯��Ԫ����organizationUnitType����Ϊnull��");
        }
        organizationStore.storeOrganizationUnitType(organizationUnitType, true);
        return organizationUnitType;
    }

    public OrganizationUnit createOrganizationUnit(OrganizationUnit
            organizationUnit) throws ObjectAlreadyExistsException,
            OrganizationException {
        if (organizationUnit == null) {
            throw new IllegalArgumentException("������֯��ԪorganizationUnit����Ϊnull��");
        }

        organizationStore.storeOrganizationUnit(organizationUnit, true);
        return organizationUnit;
    }

    public PositionType createPositionType(PositionType positionType) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (positionType == null) {
            throw new IllegalArgumentException("�����λ����positionType����Ϊnull��");
        }
        organizationStore.storePositionType(positionType, true);
        return positionType;
    }

    public Position createPosition(Position position) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (position == null) {
            throw new IllegalArgumentException("�����λposition����Ϊnull��");
        }

        organizationStore.storePosition(position, true);
        return position;
    }

    public Type createRoleType(Type roleType) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (roleType == null) {
            throw new IllegalArgumentException("�����ɫ����roleType����Ϊnull��");
        }

        organizationStore.storeRoleType(roleType, true);
        return roleType;
    }

    public Role createRole(Role role) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (role == null) {
            throw new IllegalArgumentException("�����ɫrole����Ϊnull��");
        }

        organizationStore.storeRole(role, true);
        return role;
    }

    public WorkGroupType createWorkGroupType(WorkGroupType workGroupType) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (workGroupType == null) {
            throw new IllegalArgumentException("������������workGroupType����Ϊnull��");
        }

        organizationStore.storeWorkGroupType(workGroupType, true);
        return workGroupType;
    }

    public WorkGroup createWorkGroup(WorkGroup workGroup) throws
            ObjectAlreadyExistsException, OrganizationException {
        if (workGroup == null) {
            throw new IllegalArgumentException("��������workGroup����Ϊnull��");
        }

        organizationStore.storeWorkGroup(workGroup, true);
        return workGroup;
    }

    public AbstractOrganizationObject createResponsibility(AbstractOrganizationObject responsibility) throws
            ObjectAlreadyExistsException, OrganizationException {
        organizationStore.storeResponsibility(responsibility, true);
        return responsibility;
    }

    public void addEmployeeToOrganizationUnit(Employee employee,
                                              OrganizationUnit organization) throws
            ObjectNotFoundException, OrganizationException {

        if(employee==null) {
            throw new IllegalArgumentException("�����Աemployee����Ϊnull!");
        }
        if(organization == null) {
            throw new IllegalArgumentException("������֯��Ԫorganization����Ϊnull!");
        }
        if(!organizationStore.isExistsEmployee(employee.getId())) {
            throw new ObjectNotFoundException(employee);
        }

        if(!organizationStore.isExistsOrganizationUnit(organization.getId())) {
            throw new ObjectNotFoundException(organization);
        }
        organizationStore.storeEmployeeWithOgranizationUnitRelation(employee.
                getId(), organization.getId());
    }

    public void addEmployeeToWorkGroup(Employee employee, WorkGroup workGroup,
                                       Role[] roles) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeEmployeeWithWorkGroupRelation(employee.getId(),
                workGroup.getId());
    }


    public void addPositionToOrganizationUnit(Position position,
                                              OrganizationUnit organizationUnit) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storePositionWithOrganizationUnitRelation(position.
                getId(), organizationUnit.getId());

    }

    public void addRoleToWorkGroup(Role role, WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeRoleWithWorkGroupRelation(role.getId(),
                workGroup.getId());
    }

    public void addResponsibilityToPosition(AbstractOrganizationObject responsibility,
                                            Position position) throws
            ObjectNotFoundException, OrganizationException {
        if(responsibility==null) {
            throw new IllegalArgumentException("����ְ��responsibility����Ϊnull��");
        }
        if(position == null) {
            throw new IllegalArgumentException("�����λposition����Ϊnull��");
        }
        organizationStore.storeResponsibilityWithPositonRelation(responsibility.
                getId(), position.getId());
    }

    public void addResponsibilityToRole(AbstractOrganizationObject responsibility,
                                        Role role) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeResponsibilityWithRoleRelation(responsibility.
                getId(), role.getId());
    }

    public void assignPositionToEmployee(Position position, Employee employee) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeEmployeeWithPositionRelation(employee.getId(),
                position.getId());
    }

    public void assignRoleToEmployee(Role role, Employee employee,
                                     WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeEmployeeWithRoleAndWorkGroup(employee.getId(),
                role.getId(), workGroup.getId());
    }

    public void deleteEmployee(Employee employee, boolean isTrueDeleted) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteEmployee(employee);
    }

    public void deleteOrganizationUnitType(OrganizationUnitType
                                           organizationUnitType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteOrganizationUnitType(organizationUnitType);

    }

    public void deleteOrganizationUnit(OrganizationUnit organizationUnit) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteOrganizationUnit(organizationUnit);
    }

    public void deletePostionType(PositionType positionType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deletePositionType(positionType);
    }

    public void deletePosition(Position position) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deletePosition(position);
    }

    public void deleteRoleType(Type roleType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteRoleType(roleType);
    }

    public void deleteRole(Role role) throws ObjectNotFoundException,
            OrganizationException {
        organizationStore.deleteRole(role);
    }

    public void deleteWorkGroupType(WorkGroupType workGroupType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteWorkGroupType(workGroupType);
    }

    public void deleteWorkGroup(WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteWorkGroup(workGroup);
    }

    public void deleteResponsibility(AbstractOrganizationObject responsibility) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteResponsibility(responsibility);
    }

    public void removeEmployeeFromOrganizationUnit(Employee employee,
            OrganizationUnit organization) throws ObjectNotFoundException,
            OrganizationException {
        organizationStore.deleteEmployeeWithOgranizationUnitRelation(employee.
                getId(), organization.getId());
    }

    public void removeEmployeeFromWorkGroup(Employee employee,
                                            WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteEmployeeWithWorkGroupRelation(employee.getId(),
                workGroup.getId());
    }

    public void removePositionFromOrganiztionUnit(Position position,
                                                  OrganizationUnit
                                                  organizationUnit) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deletePositionWithOrganizationUnitRelation(position.
                getId(), organizationUnit.getId());
    }

    public void removeRoleFromWorkGroup(Role role, WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteRoleWithWorkGroupRelation(role.getId(),
                workGroup.getId());
    }

    public void removeResponsibilityFromPosition(AbstractOrganizationObject responsibility,
                                                 Position position) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteResponsibilityWithPositonRelation(
                responsibility.getId(), position.getId());
    }

    public void removeResponsibilityFromRole(AbstractOrganizationObject responsibility,
                                             Role role) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteResponsibilityWithRoleRelation(responsibility.
                getId(), role.getId());
    }

    public void removeRoleFromEmployee(Role role, Employee employee,
                                       WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteEmployeeWithRoleAndWorkGroup(employee.getId(),
                role.getId(), workGroup.getId());
    }

    public void removePositionFromEmployee(Position position, Employee employee) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.deleteEmployeeWithPositionRelation(employee.getId(),
                position.getId());
    }

    public Employee modifyEmployee(Employee employee) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeEmployee(employee, false);
        return employee;
    }

    public OrganizationUnitType modifyOrganizationUnitType(OrganizationUnitType
            organizationUnitType) throws ObjectNotFoundException,
            OrganizationException {
        organizationStore.storeOrganizationUnitType(organizationUnitType, false);
        return organizationUnitType;

    }

    public OrganizationUnit modifyOrganizationUnit(OrganizationUnit
            organizationUnit) throws ObjectNotFoundException,
            OrganizationException {
        organizationStore.storeOrganizationUnit(organizationUnit, false);
        return organizationUnit;
    }

    public PositionType modifyPostionType(PositionType positionType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storePositionType(positionType, false);
        return positionType;
    }

    public Position modifyPosition(Position position) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storePosition(position, false);
        return position;
    }

    public Type modifyRoleType(Type roleType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeRoleType(roleType, false);
        return roleType;
    }

    public Role modifyRole(Role role) throws ObjectNotFoundException,
            OrganizationException {
        organizationStore.storeRole(role, false);
        return role;

    }

    public WorkGroupType modifyWorkGroupType(WorkGroupType workGroupType) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeWorkGroupType(workGroupType, false);
        return workGroupType;
    }

    public WorkGroup modifyWorkGroup(WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeWorkGroup(workGroup, false);
        return workGroup;
    }

    public AbstractOrganizationObject modifyResponsibility(AbstractOrganizationObject responsibility) throws
            ObjectNotFoundException, OrganizationException {
        organizationStore.storeResponsibility(responsibility, false);
        return responsibility;
    }

    public Employee getEmployee(String employeeid) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getEmployee(employeeid);
    }

    public OrganizationUnitType getOrganizationUnitType(String
            organiztionUnitTypeId) throws ObjectNotFoundException,
            OrganizationException {
        return organizationStore.getOrganizationUnitType(organiztionUnitTypeId);
    }

    public OrganizationUnit getOrganizationUnit(String organizationUnitId) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getOrganizationUnit(organizationUnitId);
    }

    public PositionType getPostionType(String positionTypeId) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getPostionType(positionTypeId);
    }

    public Position getPosition(String positionId) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getPosition(positionId);
    }

    public RoleType getRoleType(String roleTypeId) throws
            ObjectNotFoundException, OrganizationException {
        return (RoleType) organizationStore.getRoleType(roleTypeId);
    }

    public Role getRole(String roleId) throws ObjectNotFoundException,
            OrganizationException {
        return organizationStore.getRole(roleId);
    }

    public WorkGroupType getWorkGroupType(String workGroupTypeId) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getWorkGroupType(workGroupTypeId);
    }

    public WorkGroup getWorkGroup(String workGroupId) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.getWorkGroup(workGroupId);
    }

    public Responsibility getResponsibility(String responsibilityId) throws
            ObjectNotFoundException, OrganizationException {
        return (Responsibility) organizationStore.getResponsibility(responsibilityId);
    }

    public Employee[] getAllEmployees() throws OrganizationException {
        return organizationStore.getAllEmployees();
    }

    public OrganizationUnitType[] getAllOrganizationUnitTypes() throws
            OrganizationException {
        return organizationStore.getAllOrganizationUnitTypes();
    }

    public OrganizationUnit[] getAllOrganizationUnits() throws
            OrganizationException {
        return organizationStore.getAllOrganizationUnits();
    }

    public PositionType[] getAllPositionTypes() throws OrganizationException {
        return organizationStore.getAllPositionTypes();
    }

    public Position[] getAllPositions() throws OrganizationException {
        return organizationStore.getAllPositions();
    }

    public RoleType[] getAllRoleTypes() throws OrganizationException {
        return (RoleType[]) organizationStore.getAllRoleTypes();
    }

    public Role[] getAllRoles() throws OrganizationException {
        return organizationStore.getAllRoles();
    }

    public WorkGroupType[] getAllWorkGroupTypes() throws OrganizationException {
        return organizationStore.getAllWorkGroupTypes();
    }

    public WorkGroup[] getAllWorkGroups() throws OrganizationException {
        return organizationStore.getAllWorkGroups();
    }

    public Responsibility[] getAllResponsibilities() throws
            OrganizationException {
        return (Responsibility[]) organizationStore.getAllResponsibilities();
    }

    public Employee[] getEmployeesOfOrganizationUnit(OrganizationUnit
            organizationUnit, boolean isDirect) throws ObjectNotFoundException,
            OrganizationException {
        return null;
    }

    public OrganizationUnit[] getChildOrganizationUnits(OrganizationUnit
            superOrganizationUnit) throws ObjectNotFoundException,
            OrganizationException {
        return null;
    }

    public Position[] getPositionsOfOrganizationUnit(OrganizationUnit
            organizationUnit) throws ObjectNotFoundException,
            OrganizationException {
        return null;
    }

    public Role[] getRolesOfWorkGroup(WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Role[] getRolesOfEmployee(Employee employee, WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Position getPositionsOfEmployee(Employee employee) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public AbstractOrganizationObject[] getResponsibilitiesOfRole(Role role) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public AbstractOrganizationObject[] getResponsibilitiesOfPosition(Position position) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public boolean isEmployeeBelongsToOrganizationUnit(Employee employee,
            OrganizationUnit organizationUnit) throws ObjectNotFoundException,
            OrganizationException {
        return organizationStore.isExistsEmployeeWithOgranizationUnitRelation(
                employee.getId(), organizationUnit.getId());
    }

    public boolean isEmployeeBelongsToWorkGroup(Employee employee,
                                                WorkGroup workGroup) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.isExistsEmployeeWithWorkGroupRelation(employee.
                getId(), workGroup.getId());
    }

    public boolean isOrganizationUnitHasPosition(OrganizationUnit
                                                 organizationUnit,
                                                 Position position) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.isExistsPositionWithOrganizationUnitRelation(
                position.getId(), organizationUnit.getId());
    }

    public boolean isWorkGroupHasRole(WorkGroup workGroup, Role role) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.isExistsRoleWithWorkGroupRelation(role.getId(),
                workGroup.getId());
    }

    public boolean isPositionHasResponsibility(Position position,
                                               AbstractOrganizationObject responsibility) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.isExistsResponsibilityWithPositonRelation(
                responsibility.getId(), position.getId());
    }

    public boolean isRoleHasResponsibility(Role role,
                                           AbstractOrganizationObject responsibility) throws
            ObjectNotFoundException, OrganizationException {
        return organizationStore.isExistsResponsibilityWithRoleRelation(
                responsibility.getId(), role.getId());
    }

    public boolean isEmployeeHasRoleInsideWorkGroup(Employee employee,
            Role role, WorkGroup workGroup) throws ObjectNotFoundException,
            OrganizationException {
        return organizationStore.isExistsEmployeeWithRoleAndWorkGroup(employee.
                getId(), role.getId(), workGroup.getId());
    }

    public boolean isEmployeeHasPosition(Employee employee, Position position) throws
            ObjectNotFoundException, OrganizationException {

        return organizationStore.isExistsEmployeeWithPositionRelation(employee.
                getId(), position.getId());
    }

    public Employee[] getEmployeesOfOrganizationUnit(String organizationUnitId,
            boolean isDirect) throws ObjectNotFoundException,
            OrganizationException {
        return null;
    }

    public OrganizationUnit[] getChildOrganizationUnits(String
            organizationUnitId) throws ObjectNotFoundException,
            OrganizationException {
        return null;
    }

    public Position[] getPositionsOfOrganizationUnit(String organizationUnitId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Role[] getRolesOfWorkGroup(String workGroupId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Role[] getRolesOfEmployee(String employeeId, String workGroupId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Position[] getPositionsOfEmployee(String employeeId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Responsibility[] getResponsibilitiesOfRole(String roleId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Responsibility[] getResponsibilitiesOfPosition(String positionId) throws
            ObjectNotFoundException, OrganizationException {
        return null;
    }

    public Employee getAdministrativeDirectLeader(String employeeid) throws
            OrganizationException {
        return null;
    }

    public Employee getAdministrativeDirectLeader(Employee subordinate) throws
            OrganizationException {
        return null;
    }

    public BusinessLeader[] getBusinessDirectLeaders(String employeeid,
            String[] workgroupids) throws OrganizationException {
        return null;
    }

    public BusinessLeader[] getBusinessDirectLeaders(Employee employee,
            WorkGroup[] workGroups) throws OrganizationException {
        return null;
    }

    public void deletePosition(String positionId) throws
            ObjectNotFoundException, OrganizationException {
    }

    public void deleteRoleType(String roleTypeId) throws
            ObjectNotFoundException, OrganizationException {
    }

    public void deleteRole(String roleId) throws ObjectNotFoundException,
            OrganizationException {
    }

    public void deleteWorkGroupType(String workGroupTypeId) throws
            ObjectNotFoundException, OrganizationException {
    }

    public void deleteWorkGroup(String workGroupId) throws
            ObjectNotFoundException, OrganizationException {
    }

    public void deleteResponsibility(String responsibilityId) throws
            ObjectNotFoundException, OrganizationException {
    }

	public void addResponsibilityToPosition(Responsibility responsibility, Position position) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		
	}

	public void addResponsibilityToRole(Responsibility responsibility, Role role) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		
	}

	public Responsibility createResponsibility(Responsibility responsibility) throws ObjectAlreadyExistsException, OrganizationException {
		// TODO Auto-generated method stub
		return null;
	}

	public RoleType createRoleType(RoleType roleType) throws ObjectAlreadyExistsException, OrganizationException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isPositionHasResponsibility(Position position, Responsibility responsibility) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isRoleHasResponsibility(Role role, Responsibility responsibility) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		return false;
	}

	public Responsibility modifyResponsibility(Responsibility responsibility) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		return null;
	}

	public RoleType modifyRoleType(RoleType roleType) throws ObjectNotFoundException, InvalidRoleTypeException, OrganizationException {
		// TODO Auto-generated method stub
		return null;
	}

	public void removeResponsibilityFromPosition(Responsibility responsibility, Position position) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		
	}

	public void removeResponsibilityFromRole(Responsibility responsibility, Role role) throws ObjectNotFoundException, OrganizationException {
		// TODO Auto-generated method stub
		
	}


}
